package cn.edu.ncut.cs.springboot.petmanagementsystem.controller;

import cn.edu.ncut.cs.springboot.petmanagementsystem.common.PageResult;
import cn.edu.ncut.cs.springboot.petmanagementsystem.common.Result;
import cn.edu.ncut.cs.springboot.petmanagementsystem.pojo.Message;
import cn.edu.ncut.cs.springboot.petmanagementsystem.service.MessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * 消息通知控制器
 */
@Slf4j
@RestController
@RequestMapping("/message")
@Tag(name = "消息通知管理")
public class MessageController {

    @Autowired
    private MessageService messageService;

    /**
     * 获取用户消息列表
     */
    @Operation(summary = "获取用户消息列表")
    @GetMapping("/list")
    public Result<PageResult<Message>> getMessageList(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer page,
            @Parameter(description = "每页大小") @RequestParam(defaultValue = "10") Integer pageSize) {
        try {
            // 获取当前登录用户ID
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            Long userId = Long.parseLong(authentication.getCredentials().toString());

            PageResult<Message> result = messageService.getMessageList(userId, page, pageSize);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取用户消息列表失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 标记消息已读
     */
    @Operation(summary = "标记消息已读")
    @PutMapping("/read")
    public Result<Void> markAsRead(
            @Parameter(description = "消息ID") @RequestParam Long id) {
        try {
            // 获取当前登录用户ID
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            Long userId = Long.parseLong(authentication.getCredentials().toString());

            boolean success = messageService.markAsRead(id, userId);
            if (success) {
                return Result.success("标记已读成功");
            } else {
                return Result.error("标记已读失败");
            }
        } catch (Exception e) {
            log.error("标记消息已读失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除消息
     */
    @Operation(summary = "删除消息")
    @DeleteMapping("/{id}")
    public Result<Void> deleteMessage(
            @Parameter(description = "消息ID") @PathVariable Long id) {
        try {
            // 获取当前登录用户ID
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            Long userId = Long.parseLong(authentication.getCredentials().toString());

            boolean success = messageService.deleteMessage(id, userId);
            if (success) {
                return Result.success("删除消息成功");
            } else {
                return Result.error("删除消息失败");
            }
        } catch (Exception e) {
            log.error("删除消息失败", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取未读消息数量
     */
    @Operation(summary = "获取未读消息数量")
    @GetMapping("/unread-count")
    public Result<Integer> getUnreadCount() {
        try {
            // 获取当前登录用户ID
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            Long userId = Long.parseLong(authentication.getCredentials().toString());

            Integer count = messageService.getUnreadCount(userId);
            return Result.success(count);
        } catch (Exception e) {
            log.error("获取未读消息数量失败", e);
            return Result.error(e.getMessage());
        }
    }
}
